/*
*	Author: Shyam (shyam2347@gmail.com) 
*	Date: Feb 28, 2012
*	This file will have code for profiling..
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct _mnemFile
{
	char mnem[10];
	unsigned long long counter;
	struct _mnemFile *next;
}*mfile, *firstmfile;
typedef struct _mnemFile mnemfile;

void initmnemfile(void)
{
	mfile = NULL;
	firstmfile = NULL;
}

mnemfile * mnem_search(char []);

// I should check and insert here.. if it is already existing, then do what?
int mnemfile_insert(char mnem[])
{
        struct _mnemFile *p;
	if( (p=mnem_search(mnem)) == NULL)
	{
		//printf("Insert reg %s, count %lld\n",reg,ins_counter);
        	p = (struct _mnemFile*) malloc(sizeof(struct _mnemFile));
		strcpy(p->mnem, mnem);
		p->counter = 1;
        	p->next = NULL;
        	mfile = firstmfile;
        	if( mfile == NULL )
        	{
                	mfile = p;
                	firstmfile = mfile;
        	}
        	else
        	{
                	while(mfile->next != NULL)
                        	mfile = mfile->next;
                	mfile->next = p;
        	}
	}
	else
	{
		p->counter++;
	}
        return 0;
}

mnemfile* mnem_search(char mnem[])
{
	mnemfile *mnementry = NULL;
	mfile = firstmfile;
	if( mfile == NULL )
	{
		return mnementry;
	}
	else
	{
		while(mfile != NULL)
		{
			if( strcmp(mfile->mnem,mnem) == 0 )
			{
				return mfile;
			}
			mfile = mfile->next;
		}
	}
	return mnementry;
}

void disp_mnem()
{
	mnemfile *mnementry = NULL;
	mfile = firstmfile;
	if( mfile == NULL )
	{
		printf("Empty Mnem list\n");	
	}
	else
	{
		while(mfile != NULL)
		{
			printf("%10s \t%10lld\n", mfile->mnem, mfile->counter);
			mfile = mfile->next;
		}
	}
}
